package com.tianxia120.website.data.interceptors;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.tianxia120.website.data.annotation.IgnoreToken;
import com.tianxia120.website.data.enums.ExceptionEnum;
import com.tianxia120.website.data.utils.CommonUtils;
import com.tianxia120.website.data.utils.FieldUtils;
import com.tianxia120.website.data.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @createTime: 2018/2/11
 * @author: HingLo
 * @description: 登录认证拦截器
 */
@Slf4j
public class LoginInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        //获取controller对象
        //判断是否有权限不要权限的注解
        if (CommonUtils.hasMethodAnnotation(inv.getMethod(), IgnoreToken.class)) {
            inv.invoke();
        } else {
            Controller controller = inv.getController();
            if (controller.getSessionAttr(FieldUtils.LOGIN) == null) {
                controller.renderJson(ResultUtils.error(ExceptionEnum.TOKENINVALID));
            } else {
                inv.invoke();
            }
        }
    }
}
